package xyz.robinbingo.leetcode.a20.b06.c15;

import java.util.Arrays;

public class Solution {
    public String longestCommonPrefix(String[] strs) {
        int minL = Arrays.stream(strs).map(String::length).min(Integer::compareTo).orElse(0);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < minL; i++) {
            char c = strs[0].charAt(i);
            int finalI = i;
            if (Arrays.stream(strs).map(str -> str.charAt(finalI)).anyMatch(character -> character != c)) {
                break;
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }
}